package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.FeedbackMapper;
import com.cskaoyan.model.Feedback;
import com.cskaoyan.model.FeedbackExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.service.FeedbackService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description：实现具体意见反馈操作
 * @author     ：huzhouming
 * @date       ：2022/5/6 15:06
 */
@Transactional
@Service
public class FeedbackServiceImpl implements FeedbackService {
    @Autowired
    FeedbackMapper feedbackMapper;

    @Transactional
    @Override
    public DataVO<Feedback> query(BaseParamBO paramBO, Integer id, String username) {
        Integer page = paramBO.getPage();//页码
        Integer limit = paramBO.getLimit();//数据量
        String sort = paramBO.getSort();//排序列
        String order = paramBO.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息

        FeedbackExample feedbackExample = new FeedbackExample();
        FeedbackExample.Criteria criteria = feedbackExample.createCriteria();
        if (id != null ) {
            criteria.andUserIdEqualTo(id);
        }
        if (username != null) {
            criteria.andUsernameLike("%" + username + "%");
        }
        criteria.andDeletedEqualTo(false);
        feedbackExample.setOrderByClause(sort + " " + order);

        List<Feedback> list = feedbackMapper.selectByExample(feedbackExample);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Feedback> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return DataVO.data(total, pages, limit, page, list);
    }

    /**
     * @author: zhoukang
     * @description: TODO 添加反馈
     * @date: 2022/5/8 16:14
     */
    @Override
    public void insert(Feedback feedback) {
        //时间相关
        feedback.setAddTime(new Date());
        feedback.setUpdateTime(new Date());

        //状态相关
        feedback.setStatus(1);
        feedback.setDeleted(false);

        feedbackMapper.insert(feedback);
    }
}
